/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
/*  Route to handle authentication /auth element                                                  */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
import axios from 'axios'
import querystring from 'querystring'
import koaRouter from 'koa-router'
import svgCaptcha from 'svg-captcha'
import constants from '../utils/constants'

const router = koaRouter({
  prefix: constants.BASE_API
}) // router middleware for koa

let request = axios.create({
  baseURL: constants.LB_ADDR,
  timeout: 20000,
  credentials: 'include',
  headers: {
    'Authorization': 'Basic ' + Buffer.from('nebula-ios:d0f400535a399d70be918de9f6180d91').toString('base64'),
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})
router.post('/login', async function getAuth (ctx) {
  const user = ctx.request.body
  if (!user || !user.userName || !user.password) {
    ctx.throw(401, '用户名/密码未填写')
  }
  // if (ctx.session.captcha.toLowerCase() !== user.captcha.toLowerCase()) {
  //   ctx.throw(401, '验证码输入错误')
  // }
  try {
    const response = await request.post('/oauth/token', querystring.stringify({
      username: user.userName,
      password: user.password,
      grant_type: 'password'
    }))
    ctx.body = Object.assign({}, response.data)
    ctx.session.accessKey = response.data
  } catch (error) {
    ctx.log.error({error}, 'Call oath service failed!')
    let errMsg = '登录失败, 具体信息请联系维护人员'
    let data = null
    if ((data = error && error.response && error.response.data)) {
      errMsg = data.message || data.errors
    }
    ctx.throw(401, errMsg)
  }
})

router.get('/getMenu', async function getAuth (ctx) {
  let menus = [
    {
      id: '6',
      text: '风控报表',
      children: [
        {
          id: '6-1',
          text: '大盘情况',
          url: '/chart/spreadMap'
        },
        {
          id: '6-2',
          text: '影片投资情况列表',
          url: '/chart/investment'
        },
        {
          id: '6-3',
          text: '影片详情',
          url: '/chart/filmDetail'
        },
        {
          id: '6-4',
          text: '影片票房分布',
          url: '/chart/filmDistribution'
        },
        {
          id: '6-5',
          text: '影片资金情况(收款详情)',
          url: '/chart/filmFundDetail'
        },
        {
          id: '6-6',
          text: '影片资金情况(收款详情)轮播',
          url: '/chart/filmFundCarouselDetail'
        },
        {
          id: '6-7',
          text: '影片资金情况(付款详情)',
          url: '/chart/filmPayDetail'
        },
        {
          id: '6-8',
          text: '资金收益情况',
          url: '/chart/income'
        },
        {
          id: '6-9',
          text: '影片汇总情况',
          url: '/chart/filmCollect'
        }
      ]
    }, {
      id: '7',
      text: '风控报表(开发中)',
      children: [
        {
          id: '7-1',
          text: '全国票房分布',
          url: '/chart/spreadMapSimple'
        }
      ]
    }, {
      id: '1',
      text: '星云报表',
      children: [
        /*
        {
          id: '1-1',
          text: '在映影片票房',
          url: '/report/ticket'
        },
        {
          id: '1-2',
          text: '在映影片票房历史',
          url: '/report/filmhistory'
        },
        {
          id: '1-3',
          text: '自定义报表',
          url: '/report/combReport'
        },
        */
        {
          id: '1-4',
          text: '影片分析',
          url: '/chart/testChart'
        }
      ]
    }, {
      id: '2',
      text: '对账报表',
      children: [
        {
          id: '2-1',
          text: '会员卡充值订单对账报表',
          url: '/checkbill/cardrech'
        },
        {
          id: '2-2',
          text: '卖品订单对账报表',
          url: '/checkbill/goodorder'
        },
        {
          id: '2-3',
          text: '卖品销售对账报表',
          url: '/checkbill/goodsale'
        },
        {
          id: '2-4',
          text: '消费明细对账报表',
          url: '/checkbill/income'
        },
        {
          id: '2-5',
          text: '电影订单对账报表',
          url: '/checkbill/tickorder'
        },
        {
          id: '2-6',
          text: '第三方订单明细报表',
          url: '/checkbill/tsporder'
        }
      ]
    }, {
      id: '3',
      text: '补贴报表',
      children: [
        {
          id: '3-1',
          text: '营销活动场次票补报表',
          url: '/subsidy/ticksubs'
        },
        {
          id: '3-2',
          text: '补贴明细报表',
          url: '/subsidy/detail'
        },
        {
          id: '3-3',
          text: '补贴汇总报表',
          url: '/subsidy/marksubs'
        }
      ]
    }, {
      id: '4',
      text: '运营报表',
      children: [
        {
          id: '4-1',
          text: '会员卖品经营指标日报',
          url: '/operat/memgood'
        },
        {
          id: '4-2',
          text: '自有渠道会员活跃度日报',
          url: '/operat/activity'
        },
        {
          id: '4-3',
          text: '自有渠道卖品品项经营指标日报',
          url: '/operat/gooditem'
        },
        {
          id: '4-4',
          text: '自有渠道日报',
          url: '/operat/owner'
        },
        {
          id: '4-5',
          text: '自有渠道消费明细表',
          url: '/operat/detail'
        },
        {
          id: '4-6',
          text: '自有渠道用户日报',
          url: '/operat/user'
        },
        {
          id: '4-7',
          text: '自有渠道影片放映经营日报',
          url: '/operat/showday'
        },
        {
          id: '4-8',
          text: '自有渠道影片销售经营日报',
          url: '/operat/bookday'
        }
      ]
    }, {
      id: '5',
      text: '图表展示',
      children: [
        {
          id: '5-1',
          text: '图表示例',
          url: '/chart/chart1'
        },
        {
          id: '5-2',
          text: '自有渠道数据汇总',
          url: '/chart/collect'
        },
        {
          id: '5-3',
          text: '影城仪表板',
          url: '/chart/collectCombine'
        },
        {
          id: '5-4',
          text: '单日期仪表盘',
          url: '/chart/dateDashboard'
        },
        {
          id: '5-5',
          text: '日期区间汇总仪表盘',
          url: '/chart/dateRangeDashboard'
        },
        {
          id: '5-6',
          text: '城市日票房分析',
          url: '/chart/cityDayAnalysis'
        },
        {
          id: '5-7',
          text: '影片日票房分析',
          url: '/chart/filmDayAnalysis'
        },
        {
          id: '5-8',
          text: '影院日票房分析',
          url: '/chart/cinemaDayAnalysis'
        },
        {
          id: '5-9',
          text: '单影城日仪表板',
          url: '/chart/dateCinemaDashboard'
        }
      ]
    }
  ]
  ctx.body = menus
})

router.post('/logout', async function logout (ctx) {
  ctx.session.accessKey = null
  ctx.status = 200
})

router.get('/captcha', async function getAuth (ctx, next) {
  await next()
  let captcha = svgCaptcha.create({
    size: 4,
    noise: 1,
    height: 36,
    fontSize: 40,
    // background: '#e8f5ff',
    ignoreChars: '0oO1iIl'
  })
  ctx.session.captcha = captcha.text
  ctx.type = 'image/svg+xml'
  ctx.body = captcha.data
})
module.exports = router.routes()
